package cn.schoolwow.util.module.compare.instanceList.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instance.QueryInstance;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.module.compare.instanceList.domain.CompareInstanceListQueryOption;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SetDeleteCompareListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        CompareInstanceListQueryOption compareInstanceListQueryOption = (CompareInstanceListQueryOption) flowContext.checkData("compareInstanceListQueryOption");

        List afterFieldValueList = QueryInstanceList.newQuery(compareInstanceListQueryOption.afterInstanceList)
                .execute()
                .getMultipleFieldValueList(Arrays.asList(compareInstanceListQueryOption.addCheckFieldNames));

        List deleteCompareList = new ArrayList(compareInstanceListQueryOption.beforeInstanceList);
        deleteCompareList.removeIf(obj->{
            Object beforeValue = QueryInstance.newQuery(obj)
                    .execute()
                    .getMultipleFieldValue(compareInstanceListQueryOption.addCheckFieldNames);
            if(afterFieldValueList.contains(beforeValue)){
                return true;
            }
            return false;
        });

        flowContext.putTemporaryData("deleteCompareList", deleteCompareList);
    }

    @Override
    public String name() {
        return "设置删除实例列表";
    }
}
